package com.twetec.cms.adapter.pc.backend.exception;

import com.twetec.cms.adapter.pc.backend.response.R;
import com.twetec.cms.adapter.pc.backend.response.ResultCodeMsgEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.Set;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 未知异常处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R exceptionHandler(Exception e) {
        logger.error(ExceptionUtils.getMessage(e));
        return R.failure();
    }

    /**
     * 特定异常处理
     * 注：XXXException是特定的异常
     */
//    @ExceptionHandler(XXXException.class)
//    @ResponseBody
//    public R exceptionHandler(XXXException e) {
//        e.printStackTrace();
//        return R.failure();
//    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public R exceptionHandler(HttpMessageNotReadableException e) {
        logger.error(ExceptionUtils.getMessage(e));
        return R.resultCodeMsg(ResultCodeMsgEnum.JSON_PARSE_FAILURE);
    }

    /**
     * 请求参数缺失异常
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public R exceptionHandler(MissingServletRequestParameterException e) {
        logger.error(ExceptionUtils.getMessage(e));

        return R.failureRequestParamMiss().msg("请求参数缺失 => " + e.getParameterName());
    }

    /**
     * 请求方式错误异常
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public R exceptionHandler(HttpRequestMethodNotSupportedException e) {
        logger.error(ExceptionUtils.getMessage(e));

        Set<HttpMethod> supportedHttpMethods = e.getSupportedHttpMethods();
        HttpMethod msg = null;
        for (HttpMethod supportedHttpMethod : supportedHttpMethods) {
            msg = supportedHttpMethod;
        }

        return R.failureRequestMethod().msg("请使用" + msg.toString() + "方式提交");
    }

    /**
     * 方法参数类型不匹配异常
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public R exceptionHandler(MethodArgumentTypeMismatchException e) {
        logger.error(ExceptionUtils.getMessage(e));

        return R.failureRequestParamType().msg("方法参数类型不匹配 => " + e.getName());
    }

    /**
     * 方法参数类型不匹配异常
     */
    @ExceptionHandler(PermissionException.class)
    @ResponseBody
    public R exceptionHandler(PermissionException e) {
        logger.error(ExceptionUtils.getMessage(e));

        return R.failureUserAuth();
    }
}
